<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Controller as Controller1;
use App\Http\Requests\Api\AdminRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\JWT;

class LoginController extends Controller1
{




    public function store(AdminRequest $request){

        Admin::create($request->all());
        return $this->success('添加成功');
    }


//返回当前登录用户信息
    public function info(){
        $user = Auth::guard('admin')->user();
        return $this->success($user);
    }

    //管理员登录
    public function login(Request $request){

        $user = $request->username;

        $password = $request->password;
        $user = Admin::with('role')->where('username',$user)->first();
        if(!$user)
            return $this->failed('用户名不存在');

        $token=Auth::guard('admin')->attempt(['username'=>$request->username,'password'=>$request->password]);


        $user->token = $token;

        if($token) {
            $user->token = 'bearer '.$token;;
            return $this->setStatusCode(200)->success($user);
        }
        return $this->failed('密码错误',200);

    }


    //用户退出
    public function logout(){
        Auth::guard('admin')->logout();
        return $this->success('退出成功...');
    }



}
